import { getOrderList } from "../../api/order";
import { list as customerList } from "../../api/customer";
import {list as warehouseList} from "../../api/warehouse";
Page({

    data: {
        tab: [
            {
                text: '全部',
                index: 0,
                offsetLeft: 15,
            },
            {
                text: '制单',
                index: 1,
                offsetLeft: 56,
                state: 0

            },
            {
                text: '审核',
                index: 2,
                offsetLeft: 97,
                state: 1
            },
            {
                text: '复核',
                index: 3,
                offsetLeft: 138,
                state: 2
            },
            {
                text: '出单',
                index: 4,
                offsetLeft: 179,
                state: 3
            },
            {
                text: '下定',
                index: 5,
                offsetLeft: 220,
                state: 4
            },
            {
                text: '付款',
                index: 6,
                offsetLeft: 261,
                state: 5
            },
            {
                text: '完成',
                index: 9,
                offsetLeft: 302,
                state: 9
            },
            {
                text: '关闭',
                index: 10,
                offsetLeft: 343,
                state: 10
            },
            
            // {
            //     text: '全部',
            //     index: 0,
            //     offsetLeft: 22,
            // },
            // {
            //     text: '待接单',
            //     index: 1,
            //     offsetLeft: 78,
            //     state: 0
            // },
            // {
            //     text: '待付款',
            //     index: 2,
            //     offsetLeft: 142,
            //     state: 1
            // },
            // {
            //     text: '已付款',
            //     index: 3,
            //     offsetLeft: 205,
            //     state: 2
            // },
            // {
            //     text: '已完成',
            //     index: 4,
            //     offsetLeft: 268,
            //     state: 9
            // },
            // {
            //     text: '已关闭',
            //     index: 5,
            //     offsetLeft: 332,
            //     state: 9
            // },
        ],
        left: 15,
        activ: 0,
        buyName: '',
        companyName: '',
        storeName: '',
        triangle: '/static/triangle.png',
        triangle_Activ: '/static/triangle-activ.png',
        flag: false,
        filterActiv: null,
        testStorehouse: [
            {
                city: '全部',
                info: [
                    '宝山区石洞口库',
                    '中石化上海石化储运部',
                    '中石化闵行油库',
                    '金山区油库',
                    '玉带油库',
                    '栖霞油库',
                    '南通华盛油库',
                    '东恒油库',
                    '新世纪油库'
                ],
            },
        ],
        storeActiv: null,
        storeLev2: [

        ],
        selectName: '',
        startDate: '',
        endDate: '',
        orderState: [
            '采购中',
            '已完成',
            '临时单',
            '待付款',
            '待接单',
            '支付中',
            '待补款',
            '审核中',
            '补款中',
            '待提现',
            '提现中',
            '待划油',
            '拼团中',
            '已付款',
            '已关闭',
            '退单中',
            '退货中'
        ],
        orderStartActiv: null,
        payMethod: [
            '全款支付',
            '垫资支付',
            '保证金支付',
            '借贷支付',
            '客户欠款'
        ],
        payActiv: null,
        status:"",

        storehouseIndex:null,
        buyrActiv: null,
        storeIndex: 0,
        companyActiv: null,
        orderList:[],
        keyword:'',
        pageNum:1,
        pageSize:10,
        testBuyr: [],
        customerId:'',
        supplierList: [],
        supplierId:'',
    },
    onLoad(){
    },
    onShow() {
        this.data.orderList = []
        this.data.pageNum = 1
        this.data.pageSize = 10

        this.setData({
            orderList:[],
            pageNum: this.data.pageNum,
            pageSize:this.data.pageSize,
        })
        this.getOrder()
        this.getCustomerList()
        this.getSupplier()
        this.getWarehouseList()
    },
    getWarehouseList(){
        let params = {}
        warehouseList(params).then(res =>{
            res.data.data.unshift({
                name:'全部'
            })
            this.data.storeLev2 = res.data.data
            this.setData({
                storeLev2:this.data.storeLev2
            })
        })
    },
    onPullDownRefresh() {
        this.data.orderList = []
        this.data.pageNum = 1
        this.setData({
            pageNum:this.data.pageNum,
            orderList:this.data.orderList
        })
        this.getOrder()
        let settime = setTimeout(()=>{
            wx.stopPullDownRefresh()
            clearTimeout(settime)
        },2000)
    },
    onReachBottom() {
        this.data.pageNum =this.data.pageNum +1
        this.setData({
            pageNum:this.data.pageNum
        })
        this.getOrder()
    },
    handleQueryClear(){
        this.data.keyword = ""
        this.data.pageNum = 1
        this.data.orderList = []
        this.setData({
            keyword:this.data.keyword,
            pageNum:this.data.pageNum,
            orderList:this.data.orderList
        })
        this.getOrder()
    },
    keywordUpdate(e){
        this.data.keyword = e.detail.value
        this.setData({
            keyword: this.data.keyword
        })
        this.data.orderList = []
        this.setData({
            orderList:this.data.orderList
        })
        this.getOrder()
    },
    getOrder() {
        let params = {
            status:this.data.status,
            keyword:this.data.keyword,
            pageNum:this.data.pageNum,
            pageSize:this.data.pageSize,
            customerId:this.data.customerId,
            supplierId:this.data.supplierId,
            warehouseId:this.data.warehouseId,
            startDate:this.data.startDate,
            endDate:this.data.endDate,
        }
        getOrderList(params).then(res => {
            res.data.data.forEach(item =>{
                this.data.orderList.push(item)
            })
            this.setData({
                orderList:this.data.orderList,
            })
        })
    },
    tapTab(e) {
        let _this = this;
        if(e.currentTarget.dataset.index == 0){
            this.data.status = ""
        }
        if(e.currentTarget.dataset.index == 1){
            this.data.status = 0
        }
        if(e.currentTarget.dataset.index == 2){
            this.data.status = 1
        }
        if(e.currentTarget.dataset.index == 3){
            this.data.status = 2
        }
        if(e.currentTarget.dataset.index ==4){
            this.data.status = 3
        }
        if(e.currentTarget.dataset.index ==5){
            this.data.status = 4
        }
        if(e.currentTarget.dataset.index ==6){
            this.data.status = 5
        }
        if(e.currentTarget.dataset.index ==7){
            this.data.status = 9
        }
        if(e.currentTarget.dataset.index ==8){
            this.data.status = 10
        }
        this.data.orderList = []
        this.data.pageNum = 1
        _this.setData({
            status:this.data.status,
            activ: e.currentTarget.dataset.index,
            left: e.currentTarget.dataset.left,
            orderList: this.data.orderList,
            pageNum:this.data.pageNum,
            flag: false
        });
        this.getOrder()
    },
    popup(e) {
        let _this = this;
        let flag = !_this.data.flag;
        _this.setData({
            flag: flag,
            filterActiv: e.currentTarget.dataset.popup,
        });
    },
    sliceString(item) {
        return item.length > 4 ? item.slice(0, 4) + '...' : item;
    },
    tapBuy(e) {
        this.data.customerId = e.currentTarget.dataset.item.id
        if(e.currentTarget.dataset.item.name == '全部'){
            e.currentTarget.dataset.item.name = '采购商'
        }
        this.setData({
            buyrActiv: e.currentTarget.dataset.index,
            buyName:  e.currentTarget.dataset.item.name,
            flag: false,
            customerId: this.data.customerId,
            pageNum:1,
            pageSize:10,
            orderList:[],

        })
        this.getOrder()
    },

    buyrSearch(e){
        this.getCustomerList(e.detail.value)
    },
    supplierSearch(e){
        this.getSupplier(e.detail.value)
    },
    getCustomerList(keyword){
        let params  ={
            keyword:keyword,
            type:2
        }
        customerList(params).then(res=>{
            res.data.data.unshift ({
                name:'全部'
            })
            this.data.testBuyr = res.data.data
            this.setData({
                testBuyr:this.data.testBuyr
            })
        })
    },
    getSupplier(keyword){
        let params  ={
            keyword:keyword,
            type:1
        }
        customerList(params).then(res=>{
            res.data.data.unshift ({
                name:'全部'
            })
            this.data.supplierList = res.data.data
            this.setData({
                supplierList:this.data.supplierList
            })
        })
    },
    tapCompany(e) {
        this.data.supplierId = e.currentTarget.dataset.item.id
        if(e.currentTarget.dataset.item.name == '全部'){
            e.currentTarget.dataset.item.name = '供应商'
        }
        this.setData({
            companyActiv: e.currentTarget.dataset.index,
            companyName: e.currentTarget.dataset.item.name,
            flag: false,
            supplierId:this.data.supplierId,
            pageNum:1,
            pageSize:10,
            orderList:[],
        })
        this.getOrder()
    },
    tapStorehouse(e) {
        let storehouse_index = e.currentTarget.dataset.storehouse_index
        let index = storehouse_index != '' ? storehouse_index : 0;
        let _this = this;
            _this.setData({
            storeIndex: index,
        })
    },
    tapStorehouseItem(e) {
        let _this = this;
        if(e.currentTarget.dataset.item.name == '全部'){
            e.currentTarget.dataset.item.name = '油库'
        }
        _this.setData({
            warehouseId:e.currentTarget.dataset.item.id,
            storeActiv: e.currentTarget.dataset.item,
            storeName: e.currentTarget.dataset.item.name,
            storehouseIndex: e.currentTarget.dataset.index,
            flag: false,
            pageNum:1,
            pageSize:10,
            orderList:[],
        })
        this.getOrder()
    },
    selectDate(e) {
        let _this = this;
        let start = e.currentTarget.dataset.start;
        if (start == 1) {
            _this.setData({
                startDate:  e.detail.value
            })
        } else if (start == 2) {
            _this.setData({
                endDate: e.detail.value
            })
        }
    },
    orderStart(e) {
        console.log(e.currentTarget.dataset.startindex);
        let _this = this;
        _this.setData({
            orderStartActiv: e.currentTarget.dataset.startindex
        })
    },
    payMethod(e) {
        let _this = this;
        _this.setData({
            payActiv: e.currentTarget.dataset.pay
        })
    },
    over() {
        this.setData({
            flag: false,
            pageNum:1,
            pageSize:10,
            orderList:[],

        })
        this.getOrder()
    },
    reset() {
        this.setData({
            status:'',
            keyword:'',
            pageNum:1,
            pageSize:10,
            customerId:'',
            supplierId:'',
            warehouseId:'',
            activ: 0,
            left: 28,
            buyName:'',
            companyName:'',
            storeName:'',
            orderList:[],
            endDate: '',
            startDate: '',
            orderStartActiv: null,
            payActiv: null,
            flag: false
        })
        this.getOrder()

    },
    goToGoods(e) {
        wx.navigateTo({
          url: '/pages/order/order_details?orderId='+e.currentTarget.dataset.item.id,
        })
    },
    offMask() {
        let _this = this;
        this.setData({
            flag: false
        })
    },
    touchMove() {
        return
    }
})